import tool from '../tool'
import util from '_util/index'
import logic from '_logic'
// import Vue from 'vue'
const store = util.store
import _ from 'lodash'

// let vue = new Vue();
// const ENV=new vue.$cookie.get('tz_env');

export default{
    setLeftMenus(_this, clb = null) {
        let url = tool.getApi(_this, 'Editget', 'getMenuFields');
        let param = {
            is_set_left: 1
        };
        util.getDataCom(_this, url, param, (data) => {
            util.store.push(_this, 'setLeftMenus', 'leftMenus', false, data, 'com');
            if (clb) {
                clb(data);
            }
        });
    },
    getLeftMenus(_this, mid, flag = false) {
        let data = util.store.get(_this, 'leftMenus', 'com');

        this.hlLeftMenuConf(_this, data, mid, flag)

    },
    hlTopMenu(_this){
        let data = util.store.get(_this, 'leftMenus', 'com');
        _this.menus.map(menu => {
            let mid = menu.id;
            let mctl = menu.ctl;
            let groups = this.hlLeftMenuConf(_this, data, mid, true);
            if(groups==-1) {
                return null;
            }
            if(!groups) {
                return null;
            }
            let ctlAll = [];
            groups.forEach(group => {
                ctlAll = _.concat(ctlAll, group.ctls);
            });
            if (!ctlAll.contains(mctl)) {
                let first =groups[0]&& groups[0].ctls[0];
                menu.ctl = first || '';
            }
            return menu;

        });
    },
    hlLeftMenuConf(_this, data, mid, flag = false){
        if (!data)
            return;
        const ENV=_this.$cookie.get('txy_tz_env');
        // tool.cl('ENV:',ENV);

        let grouped = [];
        data.grouped.forEach(item => {
            grouped[item.id] = item
        });
        // tool.cl('data.mgroup[mid]:', data.mgroup[mid]);
        // tool.cl('mid:', mid);
        if (data.mgroup[mid]) {

            let {attr_group_str, group_str} = data.mgroup[mid];
            let mems = JSON.parse(attr_group_str);
            let groups = JSON.parse(group_str);

            if(!util.is_admin(_this)) {

                let auth=util.modelFn('Auth', 'get_auth_menu',_this, mid);
                if(!auth) {
                    util.auth.jump(_this);
                    return -1;
                }
                let {attr:_mems, group:_groups} = auth;

                mems =mems.map(mem=> {
                    return mem.filter((item, ind) => {
                        return _mems.contains(item);
                    })
                });

                groups=groups.filter((group, ind) => {
                    let rs=_groups.contains(group);
                    if(!rs) {
                        mems.splice(ind,1)
                    }
                    return rs;
                });
                
            }

            groups = groups.map((item, ind) => {

                let mems_tmp = mems[ind].map(mem => {
                    return grouped[mem] ? grouped[mem] : {}
                }).filter(mem => {
                    if (mem.show_env != 0) {
                        return mem.show_env == ENV;
                    }
                    return true;
                });

                // let ctls_tmp = mems[ind].map(mem => {
                //     return grouped[mem] ? grouped[mem].ctl : ''
                // }).filter(mem => {
                //     if (mem.show_env != 0) {
                //         return mem.show_env == ENV;
                //     }
                //     return true;
                // });

                let ctls_tmp = mems_tmp.map(mem=>mem.ctl||'');

                return {
                    title: item,
                    mems: mems_tmp,
                    ctls: ctls_tmp
                };
            });
            if (flag)
                return groups;
            else
                _this.groups = groups;

        } else {
            //不存在菜单时
            // _this.$router.push('/page/home')
        }
    },
    _filter_env(mem){
        if(mem.show_env!=0) {
            return mem.show_env == ENV;
        }
        return true;
    }

}
